import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SwingPopup extends JFrame implements ActionListener {
   private JTextArea output = new JTextArea();
   private JPopupMenu popup = new JPopupMenu();
   public SwingPopup() {
      super("SwingPopup");
      getContentPane().add(new JScrollPane(output), "Center");
      //menu popup
      JMenuItem mi = new JMenuItem("Recortar");
      mi.addActionListener(this);
      popup.add(mi);
      mi = new JMenuItem("Copiar");
      mi.addActionListener(this);
      popup.add(mi);
      mi = new JMenuItem("Colar");
      mi.addActionListener(this);
      popup.add(mi);
      //listener para popup
      output.addMouseListener(new PopupHandler());
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setSize(450, 260);
   }
   public void actionPerformed(ActionEvent e) {
      String acao = ((JMenuItem)e.getSource()).getText();
      if (acao.equals("Recortar")) {
         output.cut();
      } else if (acao.equals("Copiar")) {
         output.copy();
      } else {
         output.paste();
   } }
   public static void main(String[] args) {
      new SwingPopup().setVisible(true);
   }
   private class PopupHandler extends MouseAdapter {
      public void mousePressed(MouseEvent e) {
         exibirPopup(e);
      }
      public void mouseReleased(MouseEvent e) {
         exibirPopup(e);
      }
      private void exibirPopup(MouseEvent e) {
         if (e.isPopupTrigger()) {
            popup.show(e.getComponent(), e.getX(), e.getY());
} } } }
